#include "MenuScene.h"
using namespace std;

MenuScene::MenuScene(void)
{
	m_mainMenuItems[NEW_GAME]=(char*)&new_game;
	m_mainMenuItems[NEW_GAME_2_PLAYERS]=(char*)&new_game_2_players;
	m_mainMenuItems[OPTIONS]=(char*)&options;
	m_mainMenuItems[HIGHSCORES]=(char*)&highscores;
	m_mainMenuItems[QUIT]=(char*)&quit;
	m_optionsMenuItems[OPTIONS_SOUND]=(char*)&sound;
	m_optionsMenuItems[OPTIONS_MUSIC]=(char*)&music;
	m_optionsMenuItems[OPTIONS_CONTROLS]=(char*)&controls;
	m_optionsMenuItems[OPTIONS_BACK]=(char*)&back;
	m_controlsMenuItems[CONTROLS_GO_FORWARD]=(char*)&goForward;
	m_controlsMenuItems[CONTROLS_GO_DOWN]=(char*)&goDown;
	m_controlsMenuItems[CONTROLS_GO_LEFT]=(char*)&goLeft;
	m_controlsMenuItems[CONTROLS_GO_RIGHT]=(char*)&goRight;
	//m_controlsMenuItems[4]=(char*)&shoot;
	//m_controlsMenuItems[5]=(char*)&useBonus;
	m_controlsMenuItems[CONTROLS_BACK]=(char*)&back;
	m_currentMainMenuItem = 0;
	m_currentOptionsMenuItem = 0;
	m_currentControlsMenuItem = 0;
	m_bMainMenu	  = true;
	m_bOptionsMenu= false;
	m_bSoundOnOption = true;
	m_bMusicOnOption = true;
	m_bControlsMenu = false;
	m_bHighScores = false;
	mousePoint.x=0;
	mousePoint.y=0;
	mouseControl =false;
}

MenuScene::~MenuScene(void)
{
}

HRESULT MenuScene::Init(CZenEngine * zen, CZenMouse * mouse, CZenSound * m_Sound, int height, int width )
{
m_height=height;
	m_width=width;
	this->m_Sound = m_Sound;
	HRESULT res;
	//Loading alphabet
	res=zen->LoadAlphabet("res/font.bmp","res/font2.bmp",16,32);	
	if (FAILED(res))
	{
		::MessageBox(NULL, "alphabet loading!", "Error", 0);
		return E_FAIL;
	}
	m_ptrZen = zen;

	m_ptrZenMouse = mouse;

	res = m_ptrZen->LoadBitmapToSurface("res/ex1Background.bmp", &m_pBackgroundSurface, m_ptrZen->m_pDevice);
	if (FAILED(res))
	{
		::MessageBox(NULL, "background!", "Error", 0);
		return E_FAIL;
	}
	res = m_ptrZen->LoadBitmapToSurface("res/menucursor.bmp", &m_pMouseSurface, m_ptrZen->m_pDevice);
	if (FAILED(res))
	{
		::MessageBox(NULL, "background!", "Error", 0);
		return E_FAIL;
	}

	return res;
}

void MenuScene::Render()	
{
	mousePoint.x+=m_ptrZenMouse->GetMousePos().x;
	mousePoint.y+=m_ptrZenMouse->GetMousePos().y;
	
	if(mousePoint.x>m_width-20) //change later -> to window Resolution
		mousePoint.x =m_width-20;
	if(mousePoint.y>m_height-20)
		mousePoint.y=m_height-20;

	if(mousePoint.x<0) //change later -> to window Resolution
		mousePoint.x =0;
	if(mousePoint.y<0)
		mousePoint.y=0;

	D3DLOCKED_RECT LockedRect;
	HRESULT	r = m_ptrZen->m_pDevice->GetBackBuffer( 0, D3DBACKBUFFER_TYPE_MONO, &(m_ptrZen->m_pBackSurface) );
	

	//Background
	m_ptrZen->CopySurfaceToSurface(0, m_pBackgroundSurface, 0, m_ptrZen->m_pBackSurface, FALSE, D3DCOLOR_ARGB(0,0,0,0));
	// Lock the back buffer
	r = m_ptrZen->m_pBackSurface->LockRect( &LockedRect, NULL, 0 );
		
			
	// Get pointer to back buffer data
	DWORD* pData = (DWORD*)(LockedRect.pBits);
	if(m_bMainMenu)
	{
		for(int i = 0; i<MAIN_MENU_LENGTH;i++){
			m_ptrZen->PrintString(POSITION_X, POSITION_Y+i*SHIFT,m_mainMenuItems[i],true,D3DCOLOR_ARGB(0,255,0,255),pData,LockedRect.Pitch,i==m_currentMainMenuItem);
		}
	}
	else if(m_bOptionsMenu)
	{
		for(int i = 0; i<OPTIONS_MENU_LENGTH;i++)
			m_ptrZen->PrintString(POSITION_X, POSITION_Y+i*SHIFT,m_optionsMenuItems[i],true,D3DCOLOR_ARGB(0,255,0,255),pData,LockedRect.Pitch,i==m_currentOptionsMenuItem);
		if(m_bSoundOnOption)
		{
			m_ptrZen->PrintString(POSITION_X+90, POSITION_Y+OPTIONS_SOUND*SHIFT,"on",true,D3DCOLOR_ARGB(0,255,0,255),pData,LockedRect.Pitch,OPTIONS_SOUND==m_currentOptionsMenuItem);	
		}
		else
		{
			m_ptrZen->PrintString(POSITION_X +90, POSITION_Y+OPTIONS_SOUND*SHIFT,"off",true,D3DCOLOR_ARGB(0,255,0,255),pData,LockedRect.Pitch,OPTIONS_SOUND==m_currentOptionsMenuItem);	
		}

		if(m_bMusicOnOption)
		{
			m_ptrZen->PrintString(POSITION_X+90, POSITION_Y+OPTIONS_MUSIC*SHIFT,"on",true,D3DCOLOR_ARGB(0,255,0,255),pData,LockedRect.Pitch,OPTIONS_MUSIC==m_currentOptionsMenuItem);	
		}
		else
		{
			m_ptrZen->PrintString(POSITION_X + 90, POSITION_Y+OPTIONS_MUSIC*SHIFT,"off",true,D3DCOLOR_ARGB(0,255,0,255),pData,LockedRect.Pitch,OPTIONS_MUSIC==m_currentOptionsMenuItem);	
		}
	}
	else if (m_bControlsMenu)
	{
		for(int i = 0; i<CONTROLS_MENU_LENGTH;i++)
			m_ptrZen->PrintString(POSITION_X, POSITION_Y+i*SHIFT,m_controlsMenuItems[i],true,D3DCOLOR_ARGB(0,255,0,255),pData,LockedRect.Pitch,i==m_currentControlsMenuItem);
	}		
	else if (m_bHighScores)
	{
		m_ptrZen->PrintString(POSITION_X, POSITION_Y-30,(char*)back,true,D3DCOLOR_ARGB(0,255,0,255),pData,LockedRect.Pitch,true);
		m_ptrZen->PrintString(POSITION_X, POSITION_Y, "Nr  Level  Score", true, D3DCOLOR_ARGB(0,255,0,255),pData,LockedRect.Pitch,false);
		string line;
		int number = 1;
		string stringNumber;
		ifstream myfile ("highscores.txt");
		if (myfile.is_open())
		{
			while (! myfile.eof() )
			{
				ostringstream oss;
				oss  << number;
				stringNumber = oss.str();
				m_ptrZen->PrintString(POSITION_X, POSITION_Y + number * 30, (char *)stringNumber.c_str(), true, D3DCOLOR_ARGB(0,255,0,255),pData,LockedRect.Pitch,false);
				getline (myfile,line);
				char * temp = new char[line.length()+1];
				string tempNumber;
				strcpy(temp, line.c_str());
				for(unsigned int i = 0; i < line.length(); i++)
				{
					if (temp[i] == ',')
					{
						m_ptrZen->PrintString(POSITION_X + 65, POSITION_Y + number * 30, (char*)tempNumber.c_str(), true, D3DCOLOR_ARGB(0,255,0,255),pData,LockedRect.Pitch,false);
						tempNumber = "";
					}
					else
					{
						string buffer = "a";
						buffer[0] = line[i];
						tempNumber += buffer;
					}
				}
				m_ptrZen->PrintString(POSITION_X + 175, POSITION_Y + number * 30, (char*)tempNumber.c_str(), true, D3DCOLOR_ARGB(0,255,0,255),pData,LockedRect.Pitch,false);
				number ++;

			}
		}
		myfile.close();

	}

	m_ptrZen->m_pBackSurface->UnlockRect();
	//mouse cursor
	m_ptrZen->CopySurfaceToSurface(0, m_pMouseSurface,&mousePoint, m_ptrZen->m_pBackSurface, true, D3DCOLOR_ARGB(0,255,0,255));

	return;
}
bool MenuScene::HandleKeyInput(WPARAM wParam, int* gameOrMenu)
	{
		switch( wParam )
		{
			case VK_RETURN:
				if(!m_Sound->isPlayingZenSound()&&m_bSoundOnOption)
				{
					m_Sound->LoadZenSound("res/soundeffects/rocket1.wav");
					m_Sound->PlayZenSound();
				}
				if(m_bMainMenu)
				{
					if(m_currentMainMenuItem==OPTIONS)
					{
						m_bMainMenu = false;
						m_bOptionsMenu = true;
						m_bHighScores = false;
						m_currentMainMenuItem=0;
						break;
					}
					else if(m_currentMainMenuItem==QUIT)
					{
						PostQuitMessage(0);
						break;
					}
					else if(m_currentMainMenuItem==NEW_GAME)
					{						
						*gameOrMenu=1;
						return true;
						break;
					}
					else if(m_currentMainMenuItem==NEW_GAME_2_PLAYERS)
					{						
						*gameOrMenu=2;
						return true;
						break;
					}
					else if(m_currentMainMenuItem==HIGHSCORES)
					{						
						m_bMainMenu = false;
						m_bOptionsMenu = false;
						m_bHighScores = true;
						m_currentMainMenuItem=0;
						break;
					}
				}
				else if(m_bOptionsMenu)
				{
					if(m_currentOptionsMenuItem==OPTIONS_BACK)
					{
						m_bMainMenu = true;
						m_bOptionsMenu = false;
						m_bHighScores = false;
						m_currentOptionsMenuItem = 0;
						break;
					}
					else if(m_currentOptionsMenuItem==OPTIONS_SOUND)
					{
						m_bSoundOnOption=(!m_bSoundOnOption);
						break;
					}
					else if(m_currentOptionsMenuItem==OPTIONS_MUSIC)
					{
						m_bMusicOnOption=(!m_bMusicOnOption);
						break;
					}
					else if(m_currentOptionsMenuItem==OPTIONS_CONTROLS)
					{
						m_bControlsMenu=true;
						m_bOptionsMenu = false;
						m_bHighScores = false;
						m_currentOptionsMenuItem = 0;
						break;
					}
				}
				else if(m_bControlsMenu)
				{
					if(m_currentControlsMenuItem==CONTROLS_GO_FORWARD)
					{
						break;
					}
					else if(m_currentControlsMenuItem==CONTROLS_GO_DOWN)
					{
						break;
					}
					else if(m_currentControlsMenuItem==CONTROLS_GO_LEFT)
					{
						break;
					}
					else if(m_currentControlsMenuItem==CONTROLS_GO_RIGHT)
					{
						break;
					}
					else if(m_currentControlsMenuItem==CONTROLS_BACK)
					{
						m_bControlsMenu=false;
						m_bOptionsMenu = true;
						m_bHighScores = false;
						m_currentControlsMenuItem = 0;
						break;
					}	
					break;
					
				}
				else if(m_bHighScores)
				{		
					m_bMainMenu = true;
					m_bControlsMenu=false;
					m_bOptionsMenu = false;
					m_bHighScores = false;
					m_currentControlsMenuItem = 0;
				}
				break;

			case VK_ESCAPE:
				if(m_bMainMenu)
				{				
					PostQuitMessage(0);
					break;
				}
				else if(m_bOptionsMenu)
				{
						m_bMainMenu = true;
						m_bOptionsMenu = false;
						m_bHighScores = false;
						m_currentOptionsMenuItem = 0;
						break;
				}
				else if(m_bControlsMenu)
				{
					m_bControlsMenu=false;
					m_bOptionsMenu = true;
					m_bHighScores = false;
					m_currentControlsMenuItem = 0;
					break;
				}
				else if(m_bHighScores)
				{
					m_bControlsMenu=false;
					m_bOptionsMenu = false;
					m_bHighScores = false;
					m_bMainMenu = true;
					m_currentControlsMenuItem = 0;
					break;
				}
				break;

			case VK_DOWN:
				if(!m_Sound->isPlayingZenSound()&&m_bSoundOnOption)
				{
					m_Sound->LoadZenSound("res/soundeffects/click.wav");
					m_Sound->PlayZenSound();
				}
				if(m_bMainMenu)
				{
					m_currentMainMenuItem++;
					if(m_currentMainMenuItem>=MAIN_MENU_LENGTH)
						m_currentMainMenuItem=0;
				}
				else if(m_bOptionsMenu)
				{
					m_currentOptionsMenuItem++;
					if(m_currentOptionsMenuItem>=OPTIONS_MENU_LENGTH)
						m_currentOptionsMenuItem=0;
				}
				else if(m_bControlsMenu)
				{
					m_currentControlsMenuItem++;
					if(m_currentControlsMenuItem>=CONTROLS_MENU_LENGTH)
						m_currentControlsMenuItem=0;
				}
				break;

			case VK_UP:
				if(!m_Sound->isPlayingZenSound()&&m_bSoundOnOption)
				{
					m_Sound->LoadZenSound("res/soundeffects/click.wav");
					m_Sound->PlayZenSound();
				}
				if(m_bMainMenu)
				{
					m_currentMainMenuItem--;
					if(m_currentMainMenuItem<0)
						m_currentMainMenuItem=MAIN_MENU_LENGTH-1;
				}
				else if(m_bOptionsMenu)
				{
					m_currentOptionsMenuItem--;
					if(m_currentOptionsMenuItem<0)
						m_currentOptionsMenuItem=OPTIONS_MENU_LENGTH-1;
				}else if(m_bControlsMenu)
				{
					m_currentControlsMenuItem--;
					if(m_currentControlsMenuItem<0)
						m_currentControlsMenuItem=CONTROLS_MENU_LENGTH-1;
				}
				break;
			}
		
	return false ;
}

bool MenuScene::HandleMouseMove()
	{
	
	if((HandleMouseMovePoint.x!=mousePoint.x)&&(HandleMouseMovePoint.y!=mousePoint.y))
	{
		HandleMouseMovePoint.x=mousePoint.x;
		HandleMouseMovePoint.y=mousePoint.y;
		
		if(m_bHighScores){
			return false;
		}

		if(mousePoint.y<POSITION_Y + SHIFT -SHIFT/5)
		{
		if(			m_currentMainMenuItem!=0 ||
			m_currentOptionsMenuItem!=0||
			m_currentControlsMenuItem!=0){
			if(!m_Sound->isPlayingZenSound()&&m_bSoundOnOption)
				{
					m_Sound->LoadZenSound("res/soundeffects/click.wav");
					m_Sound->PlayZenSound();
				}
			}
			m_currentMainMenuItem=0;
			m_currentOptionsMenuItem=0;
			m_currentControlsMenuItem=0;
		}
		else if(mousePoint.y<POSITION_Y + 2*SHIFT-SHIFT/5)
		{
				if(			m_currentMainMenuItem!=1 ||
			m_currentOptionsMenuItem!=1||
			m_currentControlsMenuItem!=1){
			if(!m_Sound->isPlayingZenSound()&&m_bSoundOnOption)
				{
					m_Sound->LoadZenSound("res/soundeffects/click.wav");
					m_Sound->PlayZenSound();
				}
			}
			m_currentMainMenuItem=1;
			m_currentOptionsMenuItem=1;
			m_currentControlsMenuItem=1;
		}
		else if(mousePoint.y<POSITION_Y + SHIFT*3-SHIFT/5)
		{
				if(			m_currentMainMenuItem!=2 ||
			m_currentOptionsMenuItem!=2||
			m_currentControlsMenuItem!=2){
			if(!m_Sound->isPlayingZenSound()&&m_bSoundOnOption)
				{
					m_Sound->LoadZenSound("res/soundeffects/click.wav");
					m_Sound->PlayZenSound();
				}
			}
			m_currentMainMenuItem=2;
			m_currentOptionsMenuItem=2;
			m_currentControlsMenuItem=2;
		}
		else if(mousePoint.y<POSITION_Y + SHIFT*4-SHIFT/5)
		{
		if(			m_currentMainMenuItem!=3 ||
			m_currentOptionsMenuItem!=3||
			m_currentControlsMenuItem!=3){
			if(!m_Sound->isPlayingZenSound()&&m_bSoundOnOption)
				{
					m_Sound->LoadZenSound("res/soundeffects/click.wav");
					m_Sound->PlayZenSound();
				}
			}
			m_currentMainMenuItem=3;
			m_currentOptionsMenuItem=3;
			m_currentControlsMenuItem=3;
		}
		else if(mousePoint.y<POSITION_Y + SHIFT*5-SHIFT/5)
		{
				if(	m_currentOptionsMenuItem!=4||
			m_currentControlsMenuItem!=4||m_currentMainMenuItem!=4){
			if(!m_Sound->isPlayingZenSound()&&m_bSoundOnOption)
				{
					m_Sound->LoadZenSound("res/soundeffects/click.wav");
					m_Sound->PlayZenSound();
				}
			}
			m_currentMainMenuItem=4;
			m_currentOptionsMenuItem=4;
			m_currentControlsMenuItem=4;
		}
		else if(mousePoint.y<POSITION_Y + SHIFT*6-SHIFT/5)
		{
				if(	m_currentOptionsMenuItem!=5||
			m_currentControlsMenuItem!=5){
			if(!m_Sound->isPlayingZenSound()&&m_bSoundOnOption)
				{
					m_Sound->LoadZenSound("res/soundeffects/click.wav");
					m_Sound->PlayZenSound();
				}
			}
			m_currentOptionsMenuItem=5;
			m_currentControlsMenuItem=5;
		}
		else if(mousePoint.y<POSITION_Y + SHIFT*7-SHIFT/5)
		{
				if(	m_currentControlsMenuItem!=6){
			if(!m_Sound->isPlayingZenSound()&&m_bSoundOnOption)
				{
					m_Sound->LoadZenSound("res/soundeffects/click.wav");
					m_Sound->PlayZenSound();
				}
			}
			m_currentControlsMenuItem=6;
		}			
	}

	return false;
}

bool MenuScene::HandleMouseDown(int * gameOrMenu)
{
	if((mouseControl ==false)&&(m_ptrZenMouse->IsButtonDown(LEFT)))
	{
		mouseControl=true;
		return false;
	}
	if((mouseControl ==true)&&(!m_ptrZenMouse->IsButtonDown(LEFT)))
	{
		if(!m_Sound->isPlayingZenSound()&&m_bSoundOnOption)
				{
					m_Sound->LoadZenSound("res/soundeffects/rocket1.wav");
					m_Sound->PlayZenSound();
				}
		mouseControl=false;
		if(m_bMainMenu)
		{
			if(m_currentMainMenuItem==OPTIONS)
			{
				m_bMainMenu = false;
				m_bOptionsMenu = true;
				m_bHighScores = false;
				m_currentMainMenuItem=0;
			}
			else if(m_currentMainMenuItem==QUIT)
			{
				PostQuitMessage(0);

			}
			else if(m_currentMainMenuItem==NEW_GAME)
			{
				*gameOrMenu=1;
				return true;

			}
			else if(m_currentMainMenuItem==NEW_GAME_2_PLAYERS)
			{
				*gameOrMenu=2;
				return true;
			}
			else if(m_currentMainMenuItem==HIGHSCORES)
			{						
				m_bMainMenu = false;
				m_bOptionsMenu = false;
				m_bHighScores = true;
				m_currentMainMenuItem=0;
			}
		}
		else if(m_bOptionsMenu)
		{
			if(m_currentOptionsMenuItem==OPTIONS_BACK){
				m_bMainMenu = true;
				m_bOptionsMenu = false;
				m_bHighScores = false;
				m_currentOptionsMenuItem = 0;
			}
			else if(m_currentOptionsMenuItem==OPTIONS_SOUND)
			{
				m_bSoundOnOption=(!m_bSoundOnOption);
			} 
			else if(m_currentOptionsMenuItem==OPTIONS_MUSIC)
			{
				m_bMusicOnOption=(!m_bMusicOnOption);
			}
			else if(m_currentOptionsMenuItem==OPTIONS_CONTROLS)
			{
				m_bControlsMenu=true;
				m_bOptionsMenu = false;
				m_bHighScores = false;
				m_currentOptionsMenuItem = 0;
			}
		}
		else if(m_bControlsMenu)
		{
			if(m_currentControlsMenuItem==CONTROLS_GO_FORWARD)
			{}
			else if(m_currentControlsMenuItem==CONTROLS_GO_DOWN)
			{}
			else if(m_currentControlsMenuItem==CONTROLS_GO_LEFT)
			{}
			else if(m_currentControlsMenuItem==CONTROLS_GO_RIGHT)
			{}
			else if(m_currentControlsMenuItem==CONTROLS_BACK)
			{
				m_bControlsMenu=false;
				m_bOptionsMenu = true;
				m_bHighScores = false;
				m_currentControlsMenuItem = 0;
			}
		}
		else if(m_bHighScores)
		{
			m_bMainMenu = true;
			m_bControlsMenu=false;
			m_bOptionsMenu = false;
			m_bHighScores = false;
			m_currentControlsMenuItem = 0;
		}
	}

	return false;
}